package com.usian.user.service.impl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserIdEntity;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserIdEntityMapper;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserIdentityService;
import com.usian.utils.common.contants.user.AdminConstans;
import com.usian.utils.common.exception.CatchCustomException;
import com.usian.utils.common.exception.code.UserStatusCode;
//import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 身份认证接口
 */
@Service
@SuppressWarnings("ALL")
public class  ApUserIdentityServiceImpl implements ApUserIdentityService {

    @Autowired
    ApUserRealnameMapper apUserRealnameMapper;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
//    @Qualifier("")  //只能根据名字注入
//    @Resource  //先根据名字 在  类型
    private ArticleFeign articleFeign;
    @Autowired
    private ApUserIdEntityMapper apUserIdEntityMapper;


    // 方法最好只干一件事

    /**
     *
     * @param dto
     * @param status  认证通过还是不通过
     * @return
     */
    @Override
    public ResponseResult updateStatus(AuthDto dto, Short status) {
        //查询实名认证状态 9
       ApUserRealname apUserRealname =  apUserRealnameMapper.selectById(dto.getId());
       if(apUserRealname==null){
           CatchCustomException.catchs(UserStatusCode.PARAM_FAIL,"没有进行实名认证");
       }
       if(apUserRealname.getStatus() != 9){
           CatchCustomException.catchs(UserStatusCode.PARAM_FAIL,"没有进行实名认证");
       }
       //参数校验
        if (dto == null || dto.getId() == null){
            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL, "参数不合法！");
        }
        ApUserIdEntity apUserIdEntity = apUserIdEntityMapper.selectById(dto.getId());
        if (dto.getMsg() != null){
            apUserIdEntity.setStatus((short)2);
            apUserIdEntity.setReason(dto.getMsg());
            return ResponseResult.okResult(UserStatusCode.SUCCESS);
        }
        ApUserRealname apUserRealname1 = apUserRealnameMapper.selectByUserId(apUserIdEntity.getUserId());
        if (apUserRealname1 == null){
            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL, "没有实名认证");
        }
        apUserIdEntity.setStatus(status);
        apUserIdEntityMapper.updateById(apUserIdEntity);
        // 创建账号  --》 自媒体账号  --》 作者账号
//        if(createWmUserAndAuthor(apUserIdEntity.getUserId() != null))){
//            //创建自媒体账号
//            //创建作者账号
//            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL,"创建自媒体或者作者账号出错！");
//
//        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private ApUserMapper apUserMapper;

    private ResponseResult createWmUserAndAuthor(AuthDto dto){
        // 先根据身份认证的Id 查询对象 获取对象中的user_id
        //请把下面这一行代码 当成 apUserIdEntityMapper
        ApUserRealname apUserRealname =  apUserRealnameMapper.selectById(dto.getId());
        if(apUserRealname==null){
            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL,"参数有问题");
        }
        //查询user对象 --=> 因为创建自媒体账号的时候 需要把user对象中的信息 同步到wemediaUser中
        ApUser apUser =  apUserMapper.selectById(apUserRealname.getUserId());
        if(apUser==null){
            CatchCustomException.catchs(UserStatusCode.PARAM_FAIL,"参数有问题");
        }
        //创建自媒体账号
        //判断当前账号是否已经存在
        WmUser wmUser =  wemediaFeign.findByName(apUser.getName());
        if(wmUser==null){
            wmUser = new WmUser();
            wmUser.setApUserId(apUser.getId()); //关联账号
            wmUser.setCreatedTime(new Date()); //当前时间
            wmUser.setSalt("无");
            wmUser.setName(apUser.getName()); //生成的登录用户名  和 普通账号一样
            wmUser.setPassword(apUser.getPassword());//生成的登录密码  和 普通账号一样
            wmUser.setStatus(9); //账号状态
            wmUser.setType(0); //账号类型
            wmUser.setPhone(apUser.getPhone()); //手机号
            wemediaFeign.save(wmUser);
        }
        //创建作者账号
        createAuthor(wmUser);
        apUser.setFlag((short)1);
        apUserMapper.updateById(apUser);
        return null;
    }

    public void createAuthor(WmUser wmUser){
        // 只能有了自媒体账号才能有作者
        Integer user_id =  wmUser.getApUserId();
        Integer wmuser_id = wmUser.getId();
        ApAuthor apAuthor =  articleFeign.findByUserId(user_id);
        if(apAuthor==null){
            apAuthor = new ApAuthor();
            apAuthor.setUserId(user_id);
            apAuthor.setWmUserId(wmuser_id);
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(Integer.valueOf(AdminConstans.AUTHOR_TYPE)); //自媒体人类型
            articleFeign.save(apAuthor);
        }

    }


}
